<?php

session_start();
if (!empty($_SESSION["userType"])) {
    $userType = $_SESSION["userType"];
}
if (!empty($_SESSION["centreName"])) {
    $centreName = $_SESSION["centreName"];
}

$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$searchid = isset($_POST['searchid']) ? mysql_real_escape_string($_POST['searchid']) : '';
$offset = ($page - 1) * $rows;
$result = array();

include 'conn.php';
if ($userType == "System Admin") {
    if ((isset($searchid) && strpos($searchid, 'withdrawn') !== false) || ( isset($searchid) && strpos($searchid, 'Withdrawn') !== false)) {
        $rs = mysql_query("select count(*) from client where CentreId is null");
        $row = mysql_fetch_row($rs);
        $result["total"] = $row[0];
        $rs = mysql_query("Select NRIC, CustomerName, Citizenship, BeforeDeviation, AfterDeviation, DateOfBirth, Gender, 'Withdrawn'
			AS CentreName from client where CentreId is null limit $offset,$rows");
        $items = array();
        while ($row = mysql_fetch_object($rs)) {
            array_push($items, $row);
        }
        $result["rows"] = $items;
    } else {
        $rs = mysql_query("select count(*) from client where CentreId is not null");
        $row = mysql_fetch_row($rs);
        $result["total"] = $row[0];
        $rs = mysql_query("SELECT client.NRIC, client.CustomerName, client.Citizenship, client.BeforeDeviation, client.AfterDeviation, client.DateOfBirth, client.Gender, centre.CentreName
		FROM client
		INNER JOIN Centre ON client.CentreId = centre.CentreId
		WHERE client.NRIC LIKE  '%$searchid%'
                    OR client.CustomerName LIKE '%$searchid%'
		OR client.Citizenship LIKE  '%$searchid%'
		OR client.BeforeDeviation LIKE  '%$searchid%'
		OR client.AfterDeviation LIKE  '%$searchid%'
		OR client.DateOfBirth LIKE  '%$searchid%'
		OR client.Gender LIKE  '%$searchid%'
		OR centre.CentreName LIKE  '%$searchid%'
		AND centre.CentreId IS NOT NULL  limit $offset,$rows");

        $items = array();
        while ($row = mysql_fetch_object($rs)) {
            array_push($items, $row);
        }

        $rs1 = mysql_query("select count(*) from client  where CentreId is null");
        $row1 = mysql_fetch_row($rs1);
        $result1["total"] = $row1[0];
        $rs1 = mysql_query("SELECT NRIC, CustomerName, Citizenship, BeforeDeviation, AfterDeviation, DateOfBirth, Gender,  'Withdrawn' AS CentreName
		FROM client
		WHERE CentreId IS NULL AND (

		NRIC LIKE  '%$searchid%'
                    OR client.CustomerName LIKE '%$searchid%'
		OR Citizenship LIKE  '%$searchid%'
		OR BeforeDeviation LIKE  '%$searchid%'
		OR AfterDeviation LIKE  '%$searchid%'
		OR DateOfBirth LIKE  '%$searchid%'
		OR Gender LIKE  '%$searchid%'

		) limit $offset,$rows");


        $items1 = array();
        while ($row1 = mysql_fetch_object($rs1)) {
            array_push($items1, $row1);
        }

        $result["rows"] = array_merge($items, $items1);
    }
    echo json_encode($result);
} else {

    $rs = mysql_query("SELECT count(*)
FROM client
INNER JOIN Centre ON client.CentreId = centre.CentreId
WHERE centre.CentreName =  '$centreName'
AND (
client.NRIC LIKE  '%$searchid%'
    OR client.CustomerName LIKE '%$searchid%'
OR client.Citizenship LIKE  '%$searchid%'
OR client.BeforeDeviation LIKE  '%$searchid%'
OR client.AfterDeviation LIKE  '%$searchid%'
OR client.DateOfBirth LIKE  '%$searchid%'
OR client.Gender LIKE  '%$searchid%'
)");
    $row = mysql_fetch_row($rs);
    $result["total"] = $row[0];
    $rs = mysql_query("SELECT client.NRIC, client.CustomerName, client.Citizenship, client.BeforeDeviation, client.AfterDeviation, client.DateOfBirth, client.Gender, centre.CentreName
FROM client
INNER JOIN Centre ON client.CentreId = centre.CentreId
WHERE centre.CentreName =  '$centreName'
AND (
client.NRIC LIKE  '%$searchid%'
    OR client.CustomerName LIKE '%$searchid%'
OR client.Citizenship LIKE  '%$searchid%'
OR client.BeforeDeviation LIKE  '%$searchid%'
OR client.AfterDeviation LIKE  '%$searchid%'
OR client.DateOfBirth LIKE  '%$searchid%'
OR client.Gender LIKE  '%$searchid%'
) limit $offset,$rows");

    $items = array();
    while ($row = mysql_fetch_object($rs)) {
        array_push($items, $row);
    }
    $result["rows"] = $items;

    echo json_encode($result);
}


/* include 'conn.php';

  $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
  $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
  $NRIC = isset($_POST['NRIC']) ? mysql_real_escape_string($_POST['NRIC']) : '';

  $offset = ($page-1)*$rows;

  $result = array();
  $where = "client.NRIC like '$NRIC%'";
  $rs = mysql_query("select count(*) from client where " . $where);
  $row = mysql_fetch_row($rs);
  $result["total"] = $row[0];

  $rs = mysql_query("select client.NRIC, client.CustomerName, client.Citizenship,client.BeforeDeviation,
  client.AfterDeviation, client.DateOfBirth,client.Gender, centre.CentreName from client,centre where client.CentreId = centre.CentreId limit $offset,$rows");

  $items = array();
  while($row = mysql_fetch_object($rs)){
  array_push($items, $row);
  }

  $result = array();
  $where = "client.NRIC like '$NRIC%'";
  $rs = mysql_query("select count(*) from client where " . $where);
  $row = mysql_fetch_row($rs);
  $result["total"] = $row[0];

  $rs = mysql_query("select NRIC, CustomerName, Citizenship,BeforeDeviation,
  AfterDeviation, DateOfBirth,Gender, CentreId from client where CentreId is null limit $offset,$rows");

  $items1 = array();
  while($row = mysql_fetch_object($rs)){
  array_push($items, $row);
  }



  $result["rows"] = array_merge($items, $items1);

  echo json_encode($result); */
?>